import net.chrisrichardson.ec2deployer.cluster.*
import net.chrisrichardson.ec2deployer.core.*

class CloudToolsService {
  
	def grailsApplication

	ClusterManager makeClusterManager() {
		ClusterManager manager = new ClusterManager(makeAWSProperties());
		return manager;
	}

	AWSProperties makeAWSProperties() {
		File awsPropertiesFile = new File(grailsApplication.config.cloudTools.awsPropertiesFile)
		AWSProperties awsProperties = new AWSProperties(awsPropertiesFile.getAbsolutePath());
		return awsProperties;
	}

	ClusterSpec makeClusterSpec(grailsAppName)  {
		ClusterSpec clusterSpec = grailsApplication.config.cloudTools.clusterSpec
		clusterSpec.webApp(new File("explodedWar/${grailsAppName}").getAbsolutePath(), grailsAppName);
		return clusterSpec;
	}

	def withCluster(closure) {
		ClusterManager manager = makeClusterManager()
		def cluster = manager.getCluster(grailsApplication.config.cloudTools.clusterName)
		closure.call(manager, grailsApplication.config.cloudTools.clusterName, cluster)
	}

	def withClusterManager(closure) {
		ClusterManager manager = makeClusterManager()
		closure.call(manager, grailsApplication.config.cloudTools.clusterName)
	}
	
	def getAccessKey(){
		File awsPropertiesFile = new File(grailsApplication.config.cloudTools.awsPropertiesFile)
		AWSProperties awsProperties = new AWSProperties(awsPropertiesFile.getAbsolutePath());
		awsProperties.getProperty("accessKey")
	}

}
